package com.iwinding.ladygg.ui.act;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.iwinding.ladygg.R;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.RegisterCallback;
import com.iwinding.ladygg.net.request.ForeignRegisterReq;
import com.iwinding.ladygg.net.response.RegisterRp;
import com.iwinding.ladygg.utils.StringUtil;
import com.iwinding.ladygg.utils.ToastUtil;

/**
 * Created by hezhiyong on 2017/3/31.
 */

public class AbroadRegisterActivity extends BaseActivity implements View.OnClickListener {


    private AutoCompleteTextView mEtPhone;
    private AutoCompleteTextView mEtPassword;//密码
    private AutoCompleteTextView mEtPwConfirm;//确认密码

    private ImageView mImgCleanPhone;
    private ImageView mImgCleanPw;
    private ImageView mImgCleanPwConfirm;

    private Button mBtnRegister;


    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_register_abroad;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBackTxtResId(R.string.icane_register);
        setTitleResId(R.string.icane_abroad);
        initView();
        setListener();
    }

    void initView() {

        mEtPhone = (AutoCompleteTextView) findViewById(R.id.icane_edit_phone);
        mEtPassword = (AutoCompleteTextView) findViewById(R.id.icane_edit_pw);
        mEtPwConfirm = (AutoCompleteTextView) findViewById(R.id.icane_edit_pw_confirm);

        mImgCleanPhone = (ImageView) findViewById(R.id.clear_phone);
        mImgCleanPw = (ImageView) findViewById(R.id.clear_pw);
        mImgCleanPwConfirm = (ImageView) findViewById(R.id.clear_pw_confirm);

        mBtnRegister = (Button) findViewById(R.id.icane_register_now);


    }

    void setListener() {
        mEtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                mImgCleanPhone.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
            }
        });
        mEtPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImgCleanPhone.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }
        });
        mEtPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                mImgCleanPw.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
            }
        });
        mEtPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImgCleanPw.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }
        });
        mEtPwConfirm.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                mImgCleanPwConfirm.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
            }
        });
        mEtPwConfirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImgCleanPwConfirm.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }
        });

        mImgCleanPhone.setOnClickListener(this);
        mImgCleanPw.setOnClickListener(this);
        mImgCleanPwConfirm.setOnClickListener(this);

        mBtnRegister.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear_phone:
                mEtPhone.setText("");
                break;
            case R.id.clear_pw:
                mEtPassword.setText("");
                break;
            case R.id.clear_pw_confirm:
                mEtPwConfirm.setText("");
                break;
            case R.id.icane_register_now:
                register();
                break;
        }
    }


    /**
     * 注册
     */
    void register() {
        final ForeignRegisterReq registerReq = new ForeignRegisterReq();
        String phone = mEtPhone.getEditableText().toString();
        String pw = mEtPassword.getEditableText().toString();
        String pwConfirm = mEtPwConfirm.getEditableText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.openEmptyPhone(this);
            return;
        }
        if (!StringUtil.isMobileNO(phone)) {
            ToastUtil.openMolieErrorToast(this);
            return;
        }
        if (TextUtils.isEmpty(pw)) {
            ToastUtil.openEmptyPw(this);
            return;
        }
        if (TextUtils.isEmpty(pwConfirm)) {
            ToastUtil.openEmptyPwConfirm(this);
            return;
        }
        if (!TextUtils.equals(pw,pwConfirm)) {
            ToastUtil.openEmptyPwDif(this);
            return;
        }
        registerReq.uname = phone;
        registerReq.password = pw;
        registerReq.confirm = pwConfirm;

        RequestManager.postForeignRegister(mContext,registerReq, new RegisterCallback() {
            @Override
            public void onResult(RegisterRp registerRp) {
                if (!TextUtils.isEmpty(registerRp.userId)) {
                    ToastUtil.openRegisterSucess(AbroadRegisterActivity.this);
                }
            }

            @Override
            public void onFail(String errorMsg) {
                ToastUtil.openBaseToast(AbroadRegisterActivity.this,errorMsg, Toast.LENGTH_SHORT);
            }
        });
    }

}
